package com.hy.admin.security.config;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * 说明： 自定义密码加密
 */
public class MyPasswordEncoder implements PasswordEncoder{
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private String encodeType = "MD5";
	
	public MyPasswordEncoder(String encodeType) {
		if(StringUtils.isNotBlank(encodeType)) {
			this.encodeType = encodeType.trim();
		}
	}
	
	@Override
	public String encode(CharSequence rawPassword) {
		String md5Pass = DigestUtils.md5Hex(rawPassword.toString());
		return md5Pass;
	}

	@Override
	public boolean matches(CharSequence rawPassword, String encodedPassword) {
		String md5Pass = DigestUtils.md5Hex(rawPassword.toString());
		
		boolean ret = md5Pass.equals(encodedPassword);
		
		return ret;
	}

}
